package com.admin.cartAndOrder.controller;

import com.admin.cartAndOrder.pojo.BatchDelete;
import com.admin.cartAndOrder.pojo.MobileEndCart;
import com.admin.cartAndOrder.pojo.MobileEndDetails;
import com.admin.cartAndOrder.service.MobileEndCartService;
import com.admin.cartAndOrder.service.DetailsService;
import com.admin.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping({"/cart"})
@CrossOrigin
public class MobileEndCartController {
    @Autowired
    private DetailsService detailsService;
    @Autowired
    private MobileEndCartService mobileEndCartService;

    /**
     * 添加购物车
     * /cart/addCart/{productId}/{userNum}
     *
     * @param productId 商品id
     * @param userNum   用户编号
     * @return 回传添加结果
     */
    @RequestMapping({"addCart/{productId}/{userNum}"})
    public R addCart(@PathVariable("productId") Integer productId, @PathVariable("userNum") String userNum) {
        MobileEndDetails product = (MobileEndDetails) this.detailsService.getById(productId);
        MobileEndCart cart = new MobileEndCart();
        cart.setUserNum(userNum);
        cart.setProductName(product.getInfo());
        cart.setCount(1);
        cart.setPrice(product.getPrice());
        cart.setProductId(productId);
        cart.setOrderStatus(0);
        this.mobileEndCartService.save(cart);
        return R.ok();
    }

    /**
     * 通过用户编号查询当前用户购物车的购物项
     * /cart/queryAllCarts/{userNum}
     *
     * @param userNum 用户编号
     * @return 回传购物项列表 cartList 在data属性中
     */
    @RequestMapping({"queryAllCarts/{userNum}"})
    public R selectAllCart(@PathVariable String userNum) {
        return R.ok().data("cartList", this.mobileEndCartService.getAllCarts(userNum));
    }

    /**
     * 通过购物项id删除购物项
     * /cart/deleteById/{cartId}
     *
     * @param cartId 购物项id
     * @return 回传操作结果
     */
    @RequestMapping({"deleteById/{cartId}"})
    public R deleteById(@PathVariable Integer cartId) {
        boolean b = this.mobileEndCartService.removeById(cartId);
        return b ? R.ok() : R.error();
    }

    /**
     * 批量删除购物项
     * /cart/deleteByIds
     *
     * @param 传入一个名为ids的数组就可 通过json格式
     * @return 回传操作结果
     */
    @RequestMapping({"deleteByIds"})
    public R deleteById(@RequestBody BatchDelete batchDelete) {
        boolean b = this.mobileEndCartService.deleteByIds(batchDelete.getIds());
        return b ? R.ok() : R.error();
    }

    /**
     * 修改购物项数量
     * /cart/updateCart
     *
     * @param cart 修改了数量之后的购物项对象
     *             cart对象说明：只需要封装ProductId（商品id）
     *             和修改后数量 count（数量）这两个属性即可
     * @return 回传操作结果
     */
    @RequestMapping({"updateCart"})
    public R updateCart(@RequestBody MobileEndCart cart) {
        MobileEndCart cart1 = (MobileEndCart) this.mobileEndCartService.getById(cart.getId());
        cart1.setCount(cart.getCount());
        boolean b = this.mobileEndCartService.updateById(cart1);
        return b ? R.ok() : R.error();
    }

    /**
     * 根据商品编号查看购物项商品详情
     * /cart/productInfo/{productId}
     *
     * @param productId 商品编号
     * @return 回传商品对象 product在data属性中
     */
    @RequestMapping({"productInfo/{productId}"})
    public R getProductInfo(@PathVariable("productId") Integer productId) {
        MobileEndDetails details = (MobileEndDetails) this.detailsService.getById(productId);
        return R.ok().data("product", details);
    }
}
